package com.compomics.prioritizer.db.accessors;

import java.util.HashMap;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 7-nov-2008
 * Time: 9:20:20
 * To change this template use File | Settings | File Templates.
 */

public class Modification extends ModificationTableAccessor {

    /**
     * Default constructor.
     */
    public Modification() {
        super();
    }

    /**
     * Wrapper for the superclass constructor.
     *
     * @param aParams   HashMap with the parameters.
     */
    public Modification(HashMap aParams) {
        super(aParams);
    }

    /**
     * This constructor reads the modification from a resultset. The ResultSet should be positioned such that
     * a single row can be read directly (i.e., without calling the 'next()' method on the ResultSet). <br />
     * The columns should be in this order: <br />
     * Column 1: modification ID <br />
     * Column 2: name of the modification <br />
     * Column 3: mono isotopic mass <br />
     * Column 4: average mass <br />
     * Column 5: username <br />
     * Column 6: creationdate <br />
     * Column 7: modificationdate.
     *
     * @param   aRS ResultSet to read the data from.
     * @exception java.sql.SQLException    when reading the ResultSet failed.
     */
    public Modification(ResultSet aRS) throws SQLException {
        iModificationid = aRS.getLong(1);
        iName = aRS.getString(2);
        iMono_isotopic = aRS.getDouble(3);
        iAverage = aRS.getDouble(4);
        iUsername = aRS.getString(5);
        iCreationdate = (java.sql.Timestamp)aRS.getObject(6);
        iModificationdate = (java.sql.Timestamp)aRS.getObject(7);
    }

    /**
     * This methods reads all modifications from the Modification table.
     *
     * @param aConn Connection to read the modifications from.
     * @return  Modification[] with the modifications in the 'Modification' table.
     * @throws SQLException when the retrieving of the modifications went wrong.
     */
    public static Modification[] getAllModifications(Connection aConn) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification");
        ResultSet rs = prep.executeQuery();
        Vector v = new Vector();
        while(rs.next()) {
            v.add(new Modification(rs));
        }
        rs.close();
        prep.close();
        Modification[] lModifications = new Modification[v.size()];
        v.toArray(lModifications);

        return lModifications;
    }

    /**
     * This methods gets a modification  with a specific id
     *
     * @param aConn Connection to read the users from.
     * @param aId Connection to read the users from.
     * @return  User with the user in the 'User' table.
     * @throws SQLException when the retrieving of the user went wrong.
     */
    public static Modification getModificationById(Connection aConn, Long aId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select * from modification where modificationid = ?");
        prep.setLong(1, aId);
        ResultSet rs = prep.executeQuery();
        Modification lModification = null;
        while(rs.next()) {
            lModification = new Modification(rs);
        }
        rs.close();
        prep.close();

        return lModification;
    }


    /**
     * Returns a String representation for this Modification.
     *
     * @return  String  with the String representation for this Modification.
     */
    public String toString() {
        return this.iName;
    }

}

